//
//  YJYBrowsersViewController.m
//  BannerViewDemo
//
//  Created by BaizYi on 2018/11/5.
//  Copyright © 2018 BaizYi. All rights reserved.
//

#import "YJYBrowsersViewController.h"
#import "YJYPhotoCell.h"
#import "UIView+Frame.h"
#import "YJYPictureBrowsersTransitionParamer.h"
#import "YJYBrowsersInterActiveAnimationTransition.h"

static NSString *const ID = @"cell";
@interface YJYBrowsersViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, YJYPhotoCellDelegate>

@property(strong,nonatomic) UICollectionView  *collectionView;
@property(strong,nonatomic) UILabel  *pageNumLB;
@property(strong,nonatomic) UIImageView  *imageView;
@property(assign,nonatomic) CGPoint transitionImgViewCenter;


@end

@implementation YJYBrowsersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.pageNumLB];
    self.pageNumLB.text = [NSString stringWithFormat:@"%li/%li", self.animatedTransition.transitionParamer.transitionImgIndex + 1, self.dataSource.count];
    [self.pageNumLB sizeToFit];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.animatedTransition.transitionParamer.transitionImgIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(interactiveTransitionRecognizerAction:)];
    [self.view addGestureRecognizer:pan];
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YJYPhotoCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.delegate = self;
    cell.image = [UIImage imageNamed:self.dataSource[indexPath.row]];
    return cell;
}

-(void)imageViewClick:(NSInteger)cellIndex{
    [self dismissViewControllerAnimated:YES completion:nil];
}



-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.itemSize = CGSizeMake(self.view.bounds.size.width+50, self.view.bounds.size.height);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width+50, self.view.bounds.size.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor blackColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[YJYPhotoCell class] forCellWithReuseIdentifier:ID];
        
        
    }
    return _collectionView;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset = _collectionView.contentOffset.x;
    NSInteger cellIndex = offset / (SCREENWIDTH + 50);
    [self setupBaseViewControllerProperty:cellIndex];

}
-(void)setupBaseViewControllerProperty:(NSInteger)cellIndex{
    YJYPhotoCell *cell = (YJYPhotoCell *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:cellIndex inSection:0]];
    self.animatedTransition.transitionParamer.transitionImage = cell.image;
    self.animatedTransition.transitionParamer.transitionImgIndex = cellIndex;
    
    self.imageView.frame = cell.browserImageScrollView.zoomImageView.frame;
    self.imageView.image = cell.browserImageScrollView.zoomImageView.image;
    self.imageView.hidden = YES;
    self.transitionImgViewCenter = self.imageView.center;
    
    self.pageNumLB.text = [NSString stringWithFormat:@"%li/%li", cellIndex + 1, self.dataSource.count];
        [self.pageNumLB sizeToFit];
}

-(void)interactiveTransitionRecognizerAction:(UIPanGestureRecognizer *)gestureRecognizer{
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    NSLog(@"------%@", [NSValue valueWithCGPoint:translation]);
    
    CGFloat scale = 1 - (translation.y / SCREENHEIGHT);
    scale = scale < 0 ? 0 : scale;
    scale = scale > 1 ? 1 : scale;
    
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            [self setupBaseViewControllerProperty:self.animatedTransition.transitionParamer.transitionImgIndex];
            self.collectionView.hidden = YES;
            self.imageView.hidden = NO;
            
            self.animatedTransition.transitionParamer.gestureRecognizer = gestureRecognizer;
            [self dismissViewControllerAnimated:YES completion:nil];
            
            break;
        case UIGestureRecognizerStateChanged:
            self.imageView.center = CGPointMake(self.transitionImgViewCenter.x + translation.x * scale, self.transitionImgViewCenter.y + translation.y * scale);
            self.imageView.transform = CGAffineTransformMakeScale(scale, scale);
            break;
            
        case UIGestureRecognizerStateEnded:
            if (scale > 0.95) {
                [UIView animateWithDuration:0.2 animations:^{
                    self.imageView.center = self.transitionImgViewCenter;
                    self.imageView.transform = CGAffineTransformMakeScale(1, 1);
                } completion:^(BOOL finished) {
                    self.imageView.transform = CGAffineTransformIdentity;
                }];
                self.collectionView.hidden = NO;
                self.imageView.hidden = YES;
            }
            
            self.animatedTransition.transitionParamer.transitionImage = self.imageView.image;
            self.animatedTransition.transitionParamer.currentPanGesImgFrame = self.imageView.frame;
            self.animatedTransition.transitionParamer.gestureRecognizer = nil;
            
            break;
        default:
            break;
    }
    
}
-(UIImageView *)imageView{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc]init];
        [self.view addSubview:_imageView];
    }
    return _imageView;
}

-(UILabel *)pageNumLB{
    if (_pageNumLB == nil) {
        _pageNumLB = [[UILabel alloc]initWithFrame:CGRectMake(10, self.view.bounds.size.height - 30, 10, 10)];
        _pageNumLB.textColor = [UIColor whiteColor];
    }
    return _pageNumLB;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
